package com.sisgesbyte.web.managedBean.seguridad;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import org.primefaces.event.CloseEvent;
import org.primefaces.event.ToggleEvent;

import com.sisgesbyte.services.sistema.bean.AccesoTabla;

@ManagedBean
@ViewScoped
public class ControlesSeguridadMBean implements Serializable{

	private static final long serialVersionUID = 1L;
	private List<AccesoTabla> permisos;
	private boolean viewModulos =true;
	private boolean updateModulos=true;
	private boolean deleteModulos=true;
	private List<String> tablas;
	
	@PostConstruct
	public void init(String tabla){
		//cargamos las restricciones
		ExternalContext contexto = FacesContext.getCurrentInstance().getExternalContext();
		TablaRestriccionesMBean restriccionesMBean = (TablaRestriccionesMBean) contexto.getSessionMap().get("tablaRestriccionesMBean");
		permisos = restriccionesMBean.getListPermisosTabla();
		validarTablas(tabla);
		//validamos restricciones para mantenimiento
		validarMantenimiento();
		
	}

	public void onClose(CloseEvent event) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Panel Closed", "Closed panel id:'" + event.getComponent().getId() + "'");
        FacesContext.getCurrentInstance().addMessage(null, message);
    }
	public void validarTablas(String tabla){
		List<String> tb = new ArrayList<String>();
			tb.add(tabla);
			setTablas(tb);
	}
	public void validarMantenimiento(){
		for(AccesoTabla ls : permisos){
			if(tablas.contains(ls.getTabla().trim())){
				setViewModulos(ls.getVer());
				setUpdateModulos(ls.getActualizar());
				setDeleteModulos(ls.getEliminar());
			}
		}
	}
     
    public void onToggle(ToggleEvent event) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, event.getComponent().getId() + " toggled", "Status:" + event.getVisibility().name());
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

	public boolean isViewModulos() {
		return viewModulos;
	}

	public void setViewModulos(boolean viewModulos) {
		if(viewModulos){
			this.viewModulos = false;
		}else{
			this.viewModulos = true;
		}
	}

	public boolean isUpdateModulos() {
		return updateModulos;
	}

	public void setUpdateModulos(boolean updateModulos) {
		if(updateModulos){
			this.updateModulos = false;
		}else{
			this.updateModulos = true;
		}
	}

	public boolean isDeleteModulos() {
		return deleteModulos;
	}

	public void setDeleteModulos(boolean deleteModulos) {
		if(deleteModulos){
			this.deleteModulos = false;
		}else{
			this.deleteModulos = true;
		}	
	}

	public List<String> getTablas() {
		return tablas;
	}

	public void setTablas(List<String> tablas) {
		this.tablas = tablas;
	}
	
	
	

 }
